/*
 * 海上实体模块
 */
define("func/entityInfo", [
    "leaflet",
    "func/base",
    "data/ajax",
    "control/paging",
    "control/panel"
], function (L) {
    L.ICT.Func.add("EntityInfo", {
        Class: L.Class.extend({

            initialize: function () {
                this.menu = L.ict.app.menu;
                this.menuid = "ict_menu_main_stxx";
                this.ictmap = L.ict.app.ictmap;
                this.dialog = L.ict.app.util.dialog;
                this.config = Project_ParamConfig.EntityInfoConfig;
                this.ajax = new L.ICT.Ajax();
            },

            start: function () {
                if(this._popPanel) return;
                this._initVal();
                this._initUI();
                this._initEvts();
                this.menu.mainmenu.deactiveMenuExceptOne(this.menuid);
                this.menu.mainmenu.disableMenuExceptOne(this.menuid);
                this.menu.mainmenu.deactivePortInfo(); //退出港口菜单
            },

            stop: function () {
                if(this._popPanel) this._popPanel.remove();
                if(this.ictmap.map.hasLayer(this._locatelayer)){
                  this.ictmap.map.removeLayer(this._locatelayer);
                }
                this.menu.mainmenu.deactiveMenu(this.menuid);
                this.menu.mainmenu.enableMenu();
            },

            _initVal:function () {
                this._curtype = 0; // 枚举值0,1,2,3
                this._pageCtl = null;
                this._searchTxt = '';
            },

            _initUI: function () {
                var options = {
                    title: '实体信息库',
                    width: 600,
                    height: 400
                };
                options.contentHTML = this._container = $(this.getContentHTML());
                this._popPanel = new L.ICT.PopPanel(options).show();
                this._popPanel.on("popPanelRemove", function () {
                    this._popPanel = null;
                    this._container = null;
                    this.stop();
                }, this);
            },

            getContentHTML: function () {
                var html = [];
                html.push('<div class="entityinfo_container">');
                // tab标签菜单
                html.push('<ul class="nav nav-pills">');
                html.push(' <li role="presentation" class="tab_jj" data-type="1"><a href="#">军舰信息</a></li>');
                html.push(' <li role="presentation" class="tab_wgcb" data-type="2"><a href="#">违规船舶信息</a></li>');
                html.push(' <li role="presentation" class="tab_mgqy" data-type="3"><a href="#">敏感区域信息</a></li>');
                html.push('</ul>');
                html.push('<div class="infoContent">');
                // 搜索区
                html.push('<div class="input-group infosearch" style="display:none;">');
                html.push('<input type="text" class="form-control" placeholder="">');
                html.push('<span class="input-group-btn"><button class="btn btn-primary" type="button">搜索</button></span>');
                html.push('</div>');
                // 表格区
                html.push('<div class="infotabel">');
                html.push('</div>');
                // 分页区
                html.push('<div class="infopage">');
                html.push('</div>');

                html.push('</div>');
                html.push('</div>');
                return html.join('');
            },

            _initEvts: function () {
                var self = this;
                var $infoContent = this._container.find('.infoContent');
            	var $infosearch = this._container.find('.infosearch');
            	var $infotabel = this._container.find('.infotabel');
            	var $infopage = this._container.find('.infopage');

                // tab
            	this._container.find('.nav > li').on('click',function(){
            		var $this = $(this);
            		$this.addClass('active').siblings().removeClass('active');
            		var type = self._curtype = +$this.data('type');
                    $infosearch.find('input[type="text"]').val('');
                    $infotabel.html('');
                    self._searchTxt = '';
                    if (self._pageCtl){
                        self._pageCtl.remove();
                        self._pageCtl = null;
                    }
                    if (type === 1) {
                        $infosearch.css('display','table');
                        self.getWarShipInfo(self.config.warshipUrl,{
                            mmsi:-1, // -1表示查看全部
                            pageid:1, //第n页
                            pagecount:5 //每页n条
                        });
                    } else if (type === 2){
                        $infosearch.css('display','table');
                        self.getIllegalShipInfo(self.config.illegalshipUrl,{
                            mmsi:-1,
                            pageid:1,
                            pagecount:5
                        });
                    } else if (type === 3){
                        $infosearch.css('display','none');
                        self.getSensitiveAreaInfo(self.config.sensitiveareaUrl,{
                          type: 300,
                          show_status: 1,
                          pageid: 1,
                          pagecount: 5
                        });
                    }
            	});

                // search
                this._container.find('.infosearch button').on('click',function(){
                    $infotabel.html('');
                    if (self._pageCtl){
                        self._pageCtl.remove();
                        self._pageCtl = null;
                    }
                    self._searchTxt = $infosearch.find('input[type="text"]').val().trim();
                    var type = self._curtype;
                    if (type === 1) {
                        self.getWarShipInfo(self.config.warshipUrl,{
                            mmsi:self._searchTxt || -1, // -1表示查看全部
                            pageid:1, //第n页
                            pagecount:5 //每页n条
                        });
                    } else if (type === 2){
                        self.getIllegalShipInfo(self.config.illegalshipUrl,{
                            mmsi:self._searchTxt || -1,
                            pageid:1,
                            pagecount:5
                        });
                    }
                });
            },

            getWarShipInfo:function (url, data) {
                this.ajax.post(url,data,true,this,function(res,error){
                    if (this._curtype !== 1){
                        return;
                    }
                    // if (error) {
                    //     return;
                    // }
                    // if (res.state <=0){
                    //     return;
                    // }
                    this._getInfoCallback(res);
                });
            },

            getIllegalShipInfo:function (url,data){
                this.ajax.post(url,data,true,this,function(res,error){
                    if (this._curtype !== 2){
                        return;
                    }
                    // if (error) {
                    //     return;
                    // }
                    // if (res.state <=0){
                    //     return;
                    // }
                    this._getInfoCallback(res);
                });
            },

            getSensitiveAreaInfo:function (url,data) {
                this.ajax.post(url,data,true,this,function(res,error){
                    if (this._curtype !== 3){
                        return;
                    }
                    // if (error) {
                    //     return;
                    // }
                    // if (res.state <=0){
                    //     return;
                    // }
                    this._getInfoCallback(res);
                });
            },

            _getInfoCallback:function (res) {
                if (this._pageCtl){

                } else {
                    var options = {
                        total: (res && res.msg && res.msg.max_page) || 1,
                        count:5,
                        present:1,
                        pageChange: function (index){
                            if (this._curtype === 1) {
                                this.getWarShipInfo(this.config.warshipUrl,{
                                    mmsi:this._searchTxt || -1, // -1表示查看全部
                                    pageid:index, //第n页
                                    pagecount:5 //每页n条
                                });
                            } else if (this._curtype === 2){
                                this.getIllegalShipInfo(this.config.illegalshipUrl,{
                                    mmsi:this._searchTxt || -1,
                                    pageid:index,
                                    pagecount:5
                                });
                            } else if (this._curtype === 3){
                                this.getSensitiveAreaInfo(this.config.sensitiveareaUrl,{
                                    type: 300,
                                    show_status: 1,
                                    pageid: index,
                                    pagecount: 5
                                });
                            }
                        }.bind(this)
                    };
                    this._pageCtl = new L.ICT.Control.Paging(options);
                    this._pageCtl.addTo(this._container.find('.infopage')[0]);
                }
                if (this._curtype === 3) {
                  this._arealistData = res && res.msg && res.msg.areas || [];
                }
                this._container.find('.infotabel').html(this.getTabelHtml( (res && res.msg && res.msg.shipList) || (res && res.msg && res.msg.areas) || []));
                this._initTableEvts();
            },

            _initTableEvts:function (){
               var self = this;
                self._container.find('.infotabel tr:not(.thead)').on("click",function(e){
                   var mmsi = $(e.currentTarget).data('mmsi');
                   var areaid = $(e.currentTarget).data('areaid');
                   if (mmsi && self._curtype !== 3){
                      self.locateShip(mmsi);
                   } else {
                     self.locateArea(areaid);
                   }
                });
            },

            locateShip: function (mmsi) {
              var param = {};
              L.ict.app.toppanel._container.find("#search_key").val(mmsi);
              L.ict.app.toppanel._container.find(".searchType").val('MMSI');
              param.searchText = mmsi;
              param.searchType = L.ict.app.toppanel._container.find(".navbar-left .searchType").val();
              //调用search接口
              L.ICT.Func["KeySearch"].getInstance().start(param);
            },

            locateArea: function (areaid) {
              var areaInfo = this.getAreaDataById(areaid);
              if (areaInfo) {
                if(this.ictmap.map.hasLayer(this._locatelayer)){
                  this.ictmap.map.removeLayer(this._locatelayer);
                }
                this._locatelayer = L.ict.app.util.tool.convertGeoJsonToLeafletLayer(areaInfo);
                this.ictmap.map.addLayer(this._locatelayer);
                this.ictmap.map.fitBounds(this._locatelayer.getBounds());
              }
            },

            getAreaDataById:function (id) {
                if (this._arealistData && this._arealistData.length) {
                    for (var i = 0, len = this._arealistData.length; i < len; i++){
                        if (this._arealistData[i]['properties']['id'] === Number(id)) {
                            return this._arealistData[i];
                        }
                    }
                }
            },

            getTabelHtml:function(data){
                var html = [];
                html.push('<table>');
                html.push('<tr class="thead">');
                if (this._curtype === 1){
                    html.push('<td>MMSI</td><td>军舰属性</td>');
                } else if (this._curtype === 2){
                    html.push('<td>MMSI</td><td>船名</td><td>违规类型</td><td>违规性质</td>');
                } else if (this._curtype === 3){
                    html.push('<td>区域名称</td><td>区域范围</td>');
                }
                html.push('</tr>');
                for (var i = 0, len = data.length; i < len; i++){
                    var obj = data[i];
                    if (this._curtype === 1){
                        html.push('<tr class="tb" data-mmsi="' + obj.mmsi + '">');
                        html.push('<td>' + obj.mmsi + '</td>');
                        html.push('<td>' + obj.annotation + '</td>');
                        html.push('</tr>');
                    } else if (this._curtype === 2){
                        html.push('<tr class="tb" data-mmsi="' + obj.mmsi + '">');
                        html.push('<td>' + obj.mmsi + '</td>');
                        html.push('<td>' + obj.ship_name + '</td>');
                        html.push('<td>' + obj.violation + '</td>');
                        html.push('<td>' + obj.violation_class + '</td>');
                        html.push('</tr>');
                    } else if (this._curtype === 3){
                        html.push('<tr class="tb" data-areaid = "'+ obj['properties']['id'] +'">');
                        html.push('<td>'+ obj['properties']['name'] +'</td>');
                        html.push('<td>'+ obj['geometry']['coordinates'] +'</td>');
                        html.push('</tr>');
                    }
                }
                html.push('</table>');
                return html.join('');
            }
        })
    });
});
